<template>
  <div class="container">
    <el-form
      ref="formRef"
      :model="model"
      class="grid-flex"
      label-width="8rem"
      :rules="$rules"
    >
      <template v-for="(value, key) in formKeys">
        <el-form-item :key="key" :prop="key" :label="$language.local(key)">
          <xy-input v-model="model[key]" :disabled="isDisable(key)"></xy-input>
        </el-form-item>
      </template>
    </el-form>
    <div class="form-footer pb-2">
      <el-button
        size="small"
        type=""
        icon="el-icon-back"
        @click="$router.go(-1)"
        >返回</el-button
      >
      <el-button
        size="small"
        type="primary"
        icon="el-icon-check"
        class="ml-1"
        @click="onSubmit('formRef')"
      >
        保存</el-button
      >
    </div>
  </div>
</template>

<script>
import API from "../../plugins/api";
export default {
  data() {
    return {
      model: "",
      formKeys: "",
      api: "",
      disabledKey: {
        ServiceProviderName: "湖南好捷科技有限公司",
        MerchantName: "广州腾云信息科技有限公司",
        // InvoiceBusinessStatus: 2,
        // InvoiceBusinessStatusName: "已通过待开票",
        Indate: "2021-06-17T15:59:14",
        InvoiceBusinessStatusName: "",
        // ApplicationTime: "2021-06-17T15:59:14",
        Invoicesubject: "信息技术服务软件开发服务",
        Amount: 11342.36,
        Rate: 0,

        TaxAmount: 723.98,
        InvoiceTaxAmount: 12066.34,
        InvoiceType: "增值税普通发票",
      },
    };
  },

  created() {
    const { api, id } = this.$route.query;
    console.log(api, id);
    if (!api || !id) {
      this.$router.go(-1);
      return;
    }
    this.api = new API(api);
    this.formKeys = this.$store.getters.data[this.api.entity];
    this.model = this.$store.getters.data[this.api.entity + id];
  },
  methods: {
    isDisable(key) {
      const keys = Object.keys(this.disabledKey);
      return keys.includes(key);
    },
    async onSubmit(formName) {
      try {
        const valided = await this.$refs[formName].validate();

        if (!valided) {
          return;
        }
        this.model.InvoiceBusinessStatus = 3;
        await this.$http.post(this.api.apiName + this.api.action, this.model);
        this.$router.replace("/invoice_list");
      } catch (error) {
        console.error(error);
      }
    },
  },
};
</script>

<style></style>
